/** 日志 v1.0.0 **/
var kalog = {
    init: function (Boolean) {//初始化日志插件
        if (Boolean) {
            kalog.startMonitor()
        }
        var myDate = ka.dateFormat("YYYY mm-dd HH:MM:SS", new Date());
        var model_logs_star_monitor = sessionStorage.getItem("model_logs_star_monitor");
        if (model_logs_star_monitor != undefined && model_logs_star_monitor != null && model_logs_star_monitor == "1") {
            sessionStorage.setItem("model_logs", '[' + myDate + '] 已开启监控');
        }
    },
    startMonitor: function () { //开启监控
        sessionStorage.setItem("model_logs_star_monitor", "1");
        layui.layer.msg('已开启监控');
    },
    endMonitor: function () { //关闭监控
        sessionStorage.removeItem("model_logs_star_monitor");
        layui.layer.msg('关闭监控');
    },
    openLogView: function () {//打开日志监控
        if ($('#model_logs').length == 0) {
            layer.open({
                type: 1, // 页面层类型
                skin: 'layui-layer-win10', // 2.8+
                shade: false,
                offset: "lt",
                area: ['30%', '60%'],
                maxmin: false,
                title: '日志监控',
                content: [
                    '<div style="padding: 0 8px; height: 20px; line-height: 20px; border-bottom: 1px solid #F0F0F0; box-sizing: border-box; font-size: 12px;">',
                    [
                        '<a onclick="kalog.downloadLogs()" href="javascript:;">下载日志</a>',
                        '<a onclick="kalog.clearLog()" href="javascript:;">清除日志</a>',
                    ].join('   '),
                    '</div>',
                    '<pre id="model_logs" class="layui-code code-demo" lay-options="{}" style="position: absolute; top: 20px; width: 100%; height: calc(100% - 20px); padding: 6px; border: none; resize: none; overflow-y: scroll; box-sizing: border-box;"></pre>'
                ].join(''),
                success: function (layero, index, that) {
                    kaValue.ka_top_log_codeInst = layui.code({ elem: '.code-demo' });
                    var model_logs_star_monitor = sessionStorage.getItem("model_logs_star_monitor");
                    if (!(model_logs_star_monitor != undefined && model_logs_star_monitor != null && model_logs_star_monitor == "1")) {
                        sessionStorage.setItem("model_logs", "");
                    }
                    var model_logs_html = sessionStorage.getItem("model_logs");
                    if (!(model_logs_html == undefined || model_logs_html == null || model_logs_html == "")) {
                        $("#model_logs").html(model_logs_html);
                        kaValue.ka_top_log_codeInst.reload({ elem: '.code-demo' });
                    }
                },
                end: function () {
                    sessionStorage.removeItem("model_logs");
                }
            });
        }
    },
    outLogs: function (log) { //输入日志
        var logstr = "";
        for (var j in log) {
            if (typeof log[j] === "object" && log[j] !== null) {
                var cache = [];
                logstr = logstr + "\r\n" + JSON.stringify(log[j], function(key, value) {
                    if (typeof value === 'object' && value !== null) {
                        if (cache.indexOf(value) !== -1) {
                            return;
                        }
                        cache.push(value);
                    }
                    return value;
                }, '\t');
                cache = null;	//释放cache
            } else {
                logstr = logstr + " " + log[j];
            }
        }
        var myDate = ka.dateFormat("YYYY mm-dd HH:MM:SS", new Date());
        var model_logs_html = sessionStorage.getItem("model_logs");
        model_logs_html = model_logs_html + '\r\n[' + myDate + '] ' + logstr;
        var totalBytes = new Blob([model_logs_html]).size;
        var sizeInMB = totalBytes / (1024 * 1024);
        if(sizeInMB > 3){
            model_logs_html = "";
            sessionStorage.setItem("model_logs", model_logs_html);
        } else {
            sessionStorage.setItem("model_logs", model_logs_html);
        }
        if ($('#model_logs').length) {
            $("#model_logs").html(model_logs_html);
            kaValue.ka_top_log_codeInst.reload({ elem: '.code-demo' });
        }
    },
    clearLog: function () { //清除日志
        if ($('#model_logs').length) {
            $("#model_logs").html("");
        }
        sessionStorage.setItem("model_logs", "");
        kaValue.ka_top_log_codeInst.reload({ elem: '.code-demo' });
    },
    downloadLogs: function () { // 生成下载日志文件
        var logs = sessionStorage.getItem("model_logs");
        var filename = ka.dateFormat("YYYYmmddHHMMSS", new Date()) + ".txt";
        var element = document.createElement('a');
        element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(logs));
        element.setAttribute('download', filename);
        element.style.display = 'none';
        document.body.appendChild(element);
        element.click();
        document.body.removeChild(element);
    }
}
kalog.init(false);